<!DOCTYPE html>
<html lang="en">





<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" type="image/png" href="/img/favicon.png">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="My Personal Website For Blog">
  <meta name="author" content="黄宇辉">
  <meta name="keywords" content="learning note,hexo blog">
  <title>使用Dockerfile构建一个属于自己的Ubuntu - 欢迎参观小灰灰的网站哟 ヾ(◍°∇°◍)ﾉﾞ ~</title>

  <link  rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.staticfile.org/github-markdown-css/4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    <link  rel="stylesheet" href="https://cdn.staticfile.org/highlight.js/10.0.0/styles/vs2015.min.css" />
  

  


<!-- 主题依赖的图标库，不要自行修改 -->
<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_yg9cfy8wd6.css">

<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_pjno9b9zyxs.css">

<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script  src="/js/utils.js" ></script>
</head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>ANONYMOUS</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                Home
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                Archives
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                Tags
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                About
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" data-toggle="modal" data-target="#modalSearch">&nbsp;&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="view intro-2" id="background" parallax=true
         style="background: url('/img/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="container text-center white-text fadeInUp">
            <span class="h2" id="subtitle">
              
            </span>

            
              
  <div class="mt-3 post-meta">
    <i class="iconfont icon-date-fill" aria-hidden="true"></i>
    <time datetime="2020-06-26 13:15">
      June 26, 2020 pm
    </time>
  </div>


<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      2.8k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      50
       分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid">
  <div class="row">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-md">
      <div class="container nopadding-md" id="board-ctn">
        <div class="py-5" id="board">
          <div class="post-content mx-auto" id="post">
            
            <article class="markdown-body">
              <p><em>使用 Dockerfile 构建镜像并上传到 Dockerhub 的流程图如下所示</em></p>
<p><img src="/2020/06/26/%E4%BD%BF%E7%94%A8Dockerfile%E6%9E%84%E5%BB%BA%E4%B8%80%E4%B8%AA%E5%B1%9E%E4%BA%8E%E8%87%AA%E5%B7%B1%E7%9A%84Ubuntu/dockerfile-build-to-push-to-dockerhub.png" srcset="/img/loading.gif" alt=" "></p>
<h3 id="1-编写-Dockerfile"><a href="#1-编写-Dockerfile" class="headerlink" title="1.编写 Dockerfile"></a>1.编写 Dockerfile</h3><div class="hljs"><pre><code class="hljs shell">root@VM-0-4-ubuntu:/home/Dockerfile# cat Dockerfile
FROM ubuntu:18.04

MAINTAINER ishacker.net "ishacker.net@gmail.com"
<span class="hljs-meta">
#</span><span class="bash"> <span class="hljs-built_in">set</span> apt-source</span>
RUN  sed -i s@/archive.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list
<span class="hljs-meta">
#</span><span class="bash"> update apt-source</span>
RUN apt-get update -y
<span class="hljs-meta">
#</span><span class="bash"> insatll the common commands: ssh,vim,ping,ipconfig,curl..</span>
RUN apt-get -y install ssh vim curl net-tools
<span class="hljs-meta">
#</span><span class="bash"> <span class="hljs-built_in">local</span> path</span>
ENV MYPATH /usr/local
<span class="hljs-meta">
#</span><span class="bash"> work path same as <span class="hljs-built_in">local</span> path</span>
WORKDIR $MYPATH
<span class="hljs-meta">
#</span><span class="bash"> expost port</span>
EXPOSE 80
<span class="hljs-meta">
#</span><span class="bash"> ouput info</span>
CMD echo $MYPATH
CMD echo "successfully"
CMD /bin/bash</code></pre></div>
<blockquote>
<p>Docker File 中可以使用 sed 命令来批量替换软件源，因为 sed 命令在大多数环境中不用安装都可运行，而最简 Ubuntu Docker 镜像中并没有 vim.</p>
</blockquote>
<div class="hljs"><pre><code class="hljs bash"><span class="hljs-comment"># Dockerfile</span>
RUN sed -i <span class="hljs-string">'s/archive.ubuntu.com/mirrors.aliyun.com/g'</span> /etc/apt/sources.list

<span class="hljs-comment"># Ubuntu Server: no graphical interface</span>
sed -i <span class="hljs-string">'s/archive.ubuntu.com/mirrors.aliyun.com/g'</span> /etc/apt/sources.list</code></pre></div>


<h3 id="2-构建-Dockerfile"><a href="#2-构建-Dockerfile" class="headerlink" title="2. 构建 Dockerfile"></a>2. 构建 Dockerfile</h3><div class="hljs"><pre><code class="hljs shell"><span class="hljs-meta">#</span><span class="bash"> -f : dockerfile文件路径，若文件名为 Dockerfile 则可以不使用 -f 来指定文件名: docker build -t yubuntu:0.1 .</span>
<span class="hljs-meta">#</span><span class="bash"> -t : 镜像名:[版本号]</span>
root@VM-0-4-ubuntu:/home/Dockerfile# docker build -f Dockerfile -t yubuntu:0.1 . 
Sending build context to Docker daemon  2.048kB
Step 1/11 : FROM ubuntu:18.04
<span class="hljs-meta"> ---&gt;</span><span class="bash"> 8e4ce0a6ce69</span>
Step 2/11 : MAINTAINER ishacker.net "ishacker.net@gmail.com"
<span class="hljs-meta"> ---&gt;</span><span class="bash"> Using cache</span>
<span class="hljs-meta"> ---&gt;</span><span class="bash"> 3668bdb1a2c3</span>
Step 3/11 : RUN  sed -i s@/archive.ubuntu.com/@/mirrors.aliyun.com/@g /etc/apt/sources.list
<span class="hljs-meta"> ---&gt;</span><span class="bash"> Running <span class="hljs-keyword">in</span> 334aeda3e7b0</span>
Removing intermediate container 334aeda3e7b0
<span class="hljs-meta"> ---&gt;</span><span class="bash"> e5abb449e8e3</span>
Step 4/11 : RUN apt-get update -y
<span class="hljs-meta"> ---&gt;</span><span class="bash"> Running <span class="hljs-keyword">in</span> 1f339d87a93a</span>
Get:1 http://mirrors.aliyun.com/ubuntu bionic InRelease [242 kB]
Get:2 http://mirrors.aliyun.com/ubuntu bionic-updates InRelease [88.7 kB]
Get:3 http://mirrors.aliyun.com/ubuntu bionic-backports InRelease [74.6 kB]
Get:4 http://mirrors.aliyun.com/ubuntu bionic/restricted amd64 Packages [13.5 kB]
Get:5 http://mirrors.aliyun.com/ubuntu bionic/main amd64 Packages [1344 kB]
Get:6 http://mirrors.aliyun.com/ubuntu bionic/universe amd64 Packages [11.3 MB]
Get:7 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Get:8 http://mirrors.aliyun.com/ubuntu bionic/multiverse amd64 Packages [186 kB]
Get:9 http://mirrors.aliyun.com/ubuntu bionic-updates/main amd64 Packages [1270 kB]
Get:10 http://mirrors.aliyun.com/ubuntu bionic-updates/multiverse amd64 Packages [13.4 kB]
Get:11 http://mirrors.aliyun.com/ubuntu bionic-updates/restricted amd64 Packages [93.9 kB]
Get:12 http://mirrors.aliyun.com/ubuntu bionic-updates/universe amd64 Packages [1398 kB]
Get:13 http://mirrors.aliyun.com/ubuntu bionic-backports/universe amd64 Packages [8158 B]
Get:14 http://mirrors.aliyun.com/ubuntu bionic-backports/main amd64 Packages [8286 B]
Get:15 http://security.ubuntu.com/ubuntu bionic-security/multiverse amd64 Packages [9012 B]
Get:16 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [977 kB]
Get:17 http://security.ubuntu.com/ubuntu bionic-security/restricted amd64 Packages [82.2 kB]
Get:18 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [862 kB]
Fetched 18.1 MB in 9s (2070 kB/s)
Reading package lists...
Removing intermediate container 1f339d87a93a
<span class="hljs-meta"> ---&gt;</span><span class="bash"> 86a5820e7867</span>
Step 5/11 : RUN apt-get -y install ssh vim curl net-tools
<span class="hljs-meta"> ---&gt;</span><span class="bash"> Running <span class="hljs-keyword">in</span> 3e756381ba33</span>
Reading package lists...
Building dependency tree...
Reading state information...
The following additional packages will be installed:


. . . . . .


Setting up curl (7.58.0-2ubuntu3.9) ...
Setting up ssh (1:7.6p1-4ubuntu0.3) ...
Processing triggers for systemd (237-3ubuntu10.41) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
Processing triggers for ca-certificates (20190110~18.04.1) ...
Updating certificates in /etc/ssl/certs...
0 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d...
done.
Removing intermediate container 3e756381ba33
<span class="hljs-meta"> ---&gt;</span><span class="bash"> 5f1085ba9b18</span>
Step 6/11 : ENV MYPATH /usr/local
<span class="hljs-meta"> ---&gt;</span><span class="bash"> Running <span class="hljs-keyword">in</span> 80adec6bf6c1</span>
Removing intermediate container 80adec6bf6c1
<span class="hljs-meta"> ---&gt;</span><span class="bash"> 03f28e984b92</span>
Step 7/11 : WORKDIR $MYPATH
<span class="hljs-meta"> ---&gt;</span><span class="bash"> Running <span class="hljs-keyword">in</span> e71ddbb55632</span>
Removing intermediate container e71ddbb55632
<span class="hljs-meta"> ---&gt;</span><span class="bash"> 211e5936bfa4</span>
Step 8/11 : EXPOSE 80
<span class="hljs-meta"> ---&gt;</span><span class="bash"> Running <span class="hljs-keyword">in</span> 1bf8e9a3803d</span>
Removing intermediate container 1bf8e9a3803d
<span class="hljs-meta"> ---&gt;</span><span class="bash"> 2666da51b676</span>
Step 9/11 : CMD echo $MYPATH
<span class="hljs-meta"> ---&gt;</span><span class="bash"> Running <span class="hljs-keyword">in</span> 77c7906abf73</span>
Removing intermediate container 77c7906abf73
<span class="hljs-meta"> ---&gt;</span><span class="bash"> ca89ab0bf7fb</span>
Step 10/11 : CMD echo "successfully"
<span class="hljs-meta"> ---&gt;</span><span class="bash"> Running <span class="hljs-keyword">in</span> 108764aad3c6</span>
Removing intermediate container 108764aad3c6
<span class="hljs-meta"> ---&gt;</span><span class="bash"> 2fd3a5a65ab5</span>
Step 11/11 : CMD /bin/bash
<span class="hljs-meta"> ---&gt;</span><span class="bash"> Running <span class="hljs-keyword">in</span> 92f1d57f6668</span>
Removing intermediate container 92f1d57f6668
<span class="hljs-meta"> ---&gt;</span><span class="bash"> 4d37144f889a</span>
Successfully built 4d37144f889a
Successfully tagged yubuntu:0.1</code></pre></div>


<h3 id="3-测试运行"><a href="#3-测试运行" class="headerlink" title="3. 测试运行"></a>3. 测试运行</h3><div class="hljs"><pre><code class="hljs shell"><span class="hljs-meta">#</span><span class="bash"> 查看本地容器</span>
root@VM-0-4-ubuntu:/home/Dockerfile# docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
yubuntu               0.1                 4d37144f889a        9 minutes ago       249MB # 这个是刚刚使用 Dockerfile 构建的 Ubuntu
&lt;none&gt;                &lt;none&gt;              7eb46e7294d5        20 minutes ago      64.2MB
yubuntu/ubuntu        0.1                 73083828dbc5        6 hours ago         73.9MB
mytomcat              0.1                 1a8ec5515a1d        19 hours ago        652MB
ubuntu                latest              74435f89ab78        9 days ago          73.9MB
ubuntu                18.04               8e4ce0a6ce69        9 days ago          64.2MB
redis                 latest              235592615444        2 weeks ago         104MB
tomcat                9                   2eb5a120304e        2 weeks ago         647MB
tomcat                9.0                 2eb5a120304e        2 weeks ago         647MB
tomcat                latest              2eb5a120304e        2 weeks ago         647MB
nginx                 latest              2622e6cca7eb        2 weeks ago         132MB
mysql                 5.7                 9cfcce23593a        2 weeks ago         448MB
mysql                 8.0.20              be0dbf01a0f3        2 weeks ago         541MB
mysql                 latest              be0dbf01a0f3        2 weeks ago         541MB
portainer/portainer   latest              cd645f5a4769        3 weeks ago         79.1MB
hello-world           latest              bf756fb1ae65        5 months ago        13.3kB
<span class="hljs-meta">
#</span><span class="bash"> 查看当前正在运行的容器(无)</span>
root@VM-0-4-ubuntu:/home/Dockerfile# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
<span class="hljs-meta">
#</span><span class="bash"> 以后台运行的方式进入自己构建的 yubuntu 容器中</span>
root@VM-0-4-ubuntu:/home/Dockerfile# docker run -it yubuntu:0.1
root@387fba60fd69:/usr/local#
<span class="hljs-meta">
#</span><span class="bash"> 查看当前所在目录为 /usr/<span class="hljs-built_in">local</span></span>
<span class="hljs-meta">#</span><span class="bash"> 这是因为我在 Dockerfile 中将 ENV MYPATH 设置为了 /usr/<span class="hljs-built_in">local</span></span>
root@387fba60fd69:/usr/local# pwd
/usr/local
<span class="hljs-meta">
#</span><span class="bash"> 检查 vim 是否安装成功( success )</span>
root@387fba60fd69:/usr/local# vim -version
VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Mar 18 2020 18:29:15)
Garbage after option argument: "-version"
More info with: "vim -h"
<span class="hljs-meta">
#</span><span class="bash"> 检查 ssh 是否安装成功( success )</span>
root@387fba60fd69:/usr/local# ssh -V
OpenSSH_7.6p1 Ubuntu-4ubuntu0.3, OpenSSL 1.0.2n  7 Dec 2017
<span class="hljs-meta">
#</span><span class="bash"> 检查 curl 是否安装成功( success )</span>
root@387fba60fd69:/usr/local# curl -V
curl 7.58.0 (x86_64-pc-linux-gnu) libcurl/7.58.0 OpenSSL/1.1.1 zlib/1.2.11 libidn2/2.0.4 libpsl/0.19.1 (+libidn2/2.0.4) nghttp2/1.30.0 librtmp/2.3
Release-Date: 2018-01-24
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp smb smbs smtp smtps telnet tftp
Features: AsynchDNS IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz TLS-SRP HTTP2 UnixSockets HTTPS-proxy PSL
<span class="hljs-meta">
#</span><span class="bash"> 检查 net-tools 是否安装成功( success )</span>
root@387fba60fd69:/usr/local# ifconfig -version
net-tools 2.10-alpha</code></pre></div>

<p>扩 : 查看镜像的变更历史</p>
<div class="hljs"><pre><code class="hljs shell"><span class="hljs-meta">#</span><span class="bash"> 退出 yubuntu 容器</span>
root@387fba60fd69:/usr/local# exit
root@VM-0-4-ubuntu:/home/Dockerfile#
<span class="hljs-meta">
#</span><span class="bash"> 查看本地镜像</span>
root@VM-0-4-ubuntu:/home/Dockerfile# docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
yubuntu               0.1                 4d37144f889a        26 minutes ago      249MB
&lt;none&gt;                &lt;none&gt;              7eb46e7294d5        37 minutes ago      64.2MB
yubuntu/ubuntu        0.1                 73083828dbc5        7 hours ago         73.9MB
mytomcat              0.1                 1a8ec5515a1d        19 hours ago        652MB
ubuntu                latest              74435f89ab78        9 days ago          73.9MB
ubuntu                18.04               8e4ce0a6ce69        9 days ago          64.2MB
redis                 latest              235592615444        2 weeks ago         104MB
tomcat                9                   2eb5a120304e        2 weeks ago         647MB
tomcat                9.0                 2eb5a120304e        2 weeks ago         647MB
tomcat                latest              2eb5a120304e        2 weeks ago         647MB
nginx                 latest              2622e6cca7eb        2 weeks ago         132MB
mysql                 5.7                 9cfcce23593a        2 weeks ago         448MB
mysql                 8.0.20              be0dbf01a0f3        2 weeks ago         541MB
mysql                 latest              be0dbf01a0f3        2 weeks ago         541MB
portainer/portainer   latest              cd645f5a4769        3 weeks ago         79.1MB
hello-world           latest              bf756fb1ae65        5 months ago        13.3kBs
<span class="hljs-meta">
#</span><span class="bash"> 查看 yubuntu 镜像的变更历史</span>
root@VM-0-4-ubuntu:/home/Dockerfile# docker history 4d37144f889a
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
4d37144f889a        26 minutes ago      /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "/bin…   0B
2fd3a5a65ab5        26 minutes ago      /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "echo…   0B
ca89ab0bf7fb        26 minutes ago      /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "echo…   0B
2666da51b676        26 minutes ago      /bin/sh -c #(nop)  EXPOSE 80                    0B
211e5936bfa4        26 minutes ago      /bin/sh -c #(nop) WORKDIR /usr/local            0B
03f28e984b92        26 minutes ago      /bin/sh -c #(nop)  ENV MYPATH=/usr/local        0B
5f1085ba9b18        26 minutes ago      /bin/sh -c apt-get -y install ssh vim curl n…   156MB
86a5820e7867        27 minutes ago      /bin/sh -c apt-get update -y                    28.8MB
e5abb449e8e3        27 minutes ago      /bin/sh -c sed -i s@/archive.ubuntu.com/@/mi…   2.77kB
3668bdb1a2c3        37 minutes ago      /bin/sh -c #(nop)  MAINTAINER ishacker.net "…   0B
8e4ce0a6ce69        9 days ago          /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B
&lt;missing&gt;           9 days ago          /bin/sh -c mkdir -p /run/systemd &amp;&amp; echo 'do…   7B
&lt;missing&gt;           9 days ago          /bin/sh -c set -xe   &amp;&amp; echo '#!/bin/sh' &gt; /…   745B
&lt;missing&gt;           9 days ago          /bin/sh -c [ -z "$(apt-get indextargets)" ]     987kB
&lt;missing&gt;           9 days ago          /bin/sh -c #(nop) ADD file:1e8d02626176dc814…   63.2MB</code></pre></div>


<h3 id="4-将镜像发布到DockerHub"><a href="#4-将镜像发布到DockerHub" class="headerlink" title="4.将镜像发布到DockerHub"></a>4.将镜像发布到DockerHub</h3><h4 id="登录"><a href="#登录" class="headerlink" title="登录"></a>登录</h4><div class="hljs"><pre><code class="hljs shell"><span class="hljs-meta">#</span><span class="bash"> 查看 login 命令使用说明</span>
ubuntu@VM-0-4-ubuntu:~$ sudo docker login --help

Usage:  docker login [OPTIONS] [SERVER]

Log in to a Docker registry.
If no server is specified, the default is defined by the daemon.

Options:
  -p, --password string   Password
      --password-stdin    Take the password from stdin
  -u, --username string   Username
<span class="hljs-meta">
#</span><span class="bash"> 登录 DockerHubs</span>
ubuntu@VM-0-4-ubuntu:~$ sudo docker login -u yubuntu0109
Password:
WARNING! Your password will be stored unencrypted in /home/ubuntu/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
<span class="hljs-meta">
#</span><span class="bash"> 查看我的密码信息</span>
ubuntu@VM-0-4-ubuntu:~$ sudo cat /home/ubuntu/.docker/config.json
&#123;
        "auths": &#123;
                "https://index.docker.io/v1/": &#123;
                        "auth": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx==" # (●ˇ∀ˇ●) 字符马赛克嘿嘿~
                &#125;
        &#125;,
        "HttpHeaders": &#123;
                "User-Agent": "Docker-Client/19.03.11 (linux)"
        &#125;
&#125;</code></pre></div>

<h4 id="提交镜像"><a href="#提交镜像" class="headerlink" title="提交镜像"></a>提交镜像</h4><div class="hljs"><pre><code class="hljs shell"><span class="hljs-meta">#</span><span class="bash"> 查看本地镜像</span>
ubuntu@VM-0-4-ubuntu:~$ sudo docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
yubuntu               0.1                 4d37144f889a        3 hours ago         249MB
· · · · · ·
<span class="hljs-meta">
#</span><span class="bash"> 发布镜像( 但是失败了 ! )</span>
ubuntu@VM-0-4-ubuntu:~$ sudo docker push yubuntu0109/yubuntu:0.1
The push refers to repository [docker.io/yubuntu0109/yubuntu]
An image does not exist locally with the tag: yubuntu0109/yubuntu
<span class="hljs-meta">
#</span><span class="bash"> 为了解决上述异常信息，将要发布的镜像改到自己账户名下</span>
ubuntu@VM-0-4-ubuntu:~$ sudo docker tag yubuntu:0.1 yubuntu0109/yubuntu:0.1
<span class="hljs-meta">
#</span><span class="bash"> 再次尝试发布镜像( 镜像发布成功 ! )</span>
ubuntu@VM-0-4-ubuntu:~$ sudo docker push yubuntu0109/yubuntu:0.1
The push refers to repository [docker.io/yubuntu0109/yubuntu]
05165639a570: Layer already exists
5febb4f0a3d9: Layer already exists
2a9b0f5ed53e: Layer already exists
ddc500d84994: Layer already exists
c64c52ea2c16: Layer already exists
5930c9e5703f: Layer already exists
b187ff70b2e4: Layer already exists
0.1: digest: sha256:81ca2b6320b4f972bda75a800e0f7a23b50691790578079298d6de325bcacb4a size: 1783</code></pre></div>

<p><em>如果你当你执行 <code>docker push yubuntu0109/yubuntu:0.1</code> 时抛出 <code>denied: requested access to the resource is denied</code> 或 <code>An image does not exist locally with the tag: yubuntu0109/yubuntu</code> 错误提示，请参考如下解决方案 :</em></p>
<div class="hljs"><pre><code class="hljs shell"><span class="hljs-meta">#</span><span class="bash"> 首先将要发布的镜像改到自己账户名下</span>
<span class="hljs-meta">#</span><span class="bash"> tag标签如果不写，默认为latest</span>
ubuntu@VM-0-4-ubuntu:~$ sudo docker tag yubuntu:0.1 yubuntu0109/yubuntu:0.1
<span class="hljs-meta">
#</span><span class="bash"> 再次查看本地镜像，会发现镜像名已经修改成功</span>
ubuntu@VM-0-4-ubuntu:~$ sudo docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
yubuntu               0.1                 4d37144f889a        3 hours ago         249MB #( x )
yubuntu0109/yubuntu   0.1                 4d37144f889a        3 hours ago         249MB #( v )
· · · · · ·
<span class="hljs-meta">
#</span><span class="bash"> 然后再次发布镜像即可</span>
ubuntu@VM-0-4-ubuntu:~$ sudo docker push yubuntu0109/yubuntu:0.1</code></pre></div>

<p>镜像发布成功后，打开 <a href="https://hub.docker.com/u/yubuntu0109" target="_blank" rel="noopener">https://hub.docker.com/u/yubuntu0109</a> 会发现镜像已经成功发布到自己的 DockerHub 上，如下图所示 :</p>
<p><img src="/2020/06/26/%E4%BD%BF%E7%94%A8Dockerfile%E6%9E%84%E5%BB%BA%E4%B8%80%E4%B8%AA%E5%B1%9E%E4%BA%8E%E8%87%AA%E5%B7%B1%E7%9A%84Ubuntu/dockerhub-first-image-ubuntu18.04.PNG" srcset="/img/loading.gif" alt=" "></p>
<h3 id="5-从-Dockerhub-拉取并运行镜像"><a href="#5-从-Dockerhub-拉取并运行镜像" class="headerlink" title="5.从 Dockerhub 拉取并运行镜像"></a>5.从 Dockerhub 拉取并运行镜像</h3><div class="hljs"><pre><code class="hljs shell"><span class="hljs-meta">#</span><span class="bash"> 查看本地镜像</span>
ubuntu@VM-0-4-ubuntu:~$ sudo docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
yubuntu0109/yubuntu   0.1                 4d37144f889a        18 hours ago        249MB
. . . . . .
<span class="hljs-meta">
#</span><span class="bash"> 查看正在运行的容器(无)</span>
ubuntu@VM-0-4-ubuntu:~$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
<span class="hljs-meta">
#</span><span class="bash"> 删除自己制作的 yubuntu 容器</span>
ubuntu@VM-0-4-ubuntu:~$ sudo docker rmi -f yubuntu0109/yubuntu:0.1
Untagged: yubuntu0109/yubuntu:0.1
Untagged: yubuntu0109/yubuntu@sha256:81ca2b6320b4f972bda75a800e0f7a23b50691790578079298d6de325bcacb4a
Deleted: sha256:4d37144f889a2da39e342ce87c88bb8d20de7057b87e7417d46688b37124cea5
Deleted: sha256:2fd3a5a65ab5b63fb45de0f6089cb4ccb00abf5025deaa218e96efa240d8c684
Deleted: sha256:ca89ab0bf7fbb547c2119a1c8a5d85b5e6fcf06904dbef32ef8d02831917dce3
Deleted: sha256:2666da51b676414e2b60e1095df2d3f0fffd842b7d2e7a2398fa0603747e8fc6
Deleted: sha256:211e5936bfa424098940b1e35aa7c171ca2a3c54ecc5494a91c58ee3e61f21f7
Deleted: sha256:03f28e984b924015cb3f1c00dd14dec7ba82835db53d5325638886ba80707e51
Deleted: sha256:5f1085ba9b18716e374608785d6ba143cdb159139279787991321b57521dff74
Deleted: sha256:86a5820e78671222d8e586c326fea01f31446c76818967b5203c5342852aa170
Deleted: sha256:e5abb449e8e34aa30a3459373e30d6df090c9776918de3ea10f67f4fb15e71c8
<span class="hljs-meta">
#</span><span class="bash"> 再次查看本地镜像，发现已成功删除 yubuntu 镜像</span>
ubuntu@VM-0-4-ubuntu:~$ sudo docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE

---
<span class="hljs-meta">
#</span><span class="bash"> 搜索 yubuntu 镜像</span>
ubuntu@VM-0-4-ubuntu:~$ sudo docker search yubuntu
NAME                  DESCRIPTION                               STARS               OFFICIAL            AUTOMATED
yubuntu0109/yubuntu   
mmusthafapt/yubuntu                                             0
yushiwei/yubuntu                                                0
<span class="hljs-meta">
#</span><span class="bash"> 从 DockerHub 拉取自己刚上传的 yubuntu 镜像</span>
ubuntu@VM-0-4-ubuntu:~$ sudo docker pull yubuntu0109/yubuntu:0.1
0.1: Pulling from yubuntu0109/yubuntu
d7c3167c320d: Already exists
131f805ec7fd: Already exists
322ed380e680: Already exists
6ac240b13098: Already exists
4f893608b542: Already exists
38b674ba7d2c: Already exists
4e64cc6583b3: Already exists
Digest: sha256:81ca2b6320b4f972bda75a800e0f7a23b50691790578079298d6de325bcacb4a
Status: Downloaded newer image for yubuntu0109/yubuntu:0.1
docker.io/yubuntu0109/yubuntu:0.1
<span class="hljs-meta">
#</span><span class="bash"> 查看 yubuntu 镜像是否拉取成功</span>
ubuntu@VM-0-4-ubuntu:~$ sudo docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
yubuntu0109/yubuntu   0.1                 4d37144f889a        19 hours ago        249MB
<span class="hljs-meta">
#</span><span class="bash"> 查看当前正在运行的容器(无)</span>
ubuntu@VM-0-4-ubuntu:~$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
<span class="hljs-meta">
#</span><span class="bash"> 以后台运行的形式启动 yubuntu 容器</span>
ubuntu@VM-0-4-ubuntu:~$ sudo docker run --name MyUbuntu18.04 -it -d yubuntu0109/yubuntu:0.1
c39df819132fb74093fac04957fa498922759b0949837d7baeb4d605df30d683
<span class="hljs-meta">
#</span><span class="bash"> 查看 yubuntu 容器是否成功在后台启动</span>
ubuntu@VM-0-4-ubuntu:~$ sudo docker ps
CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS              PORTS            NAMES
c39df819132f        yubuntu0109/yubuntu:0.1   "/bin/sh -c /bin/bash"   5 seconds ago       Up 2 seconds        80/tcp           MyUbuntu18.04
<span class="hljs-meta">
#</span><span class="bash"> 进入正在运行的 yubuntu 容器中</span>
ubuntu@VM-0-4-ubuntu:~$ sudo docker exec -it MyUbuntu18.04 /bin/bash
root@c39df819132f:/usr/local# ls
bin  etc  games  include  lib  man  sbin  share  src
root@c39df819132f:/usr/local# exit
exit
<span class="hljs-meta">
#</span><span class="bash"> 关闭 yubuntu 容器</span>
ubuntu@VM-0-4-ubuntu:~$ sudo docker ps
CONTAINER ID        IMAGE                     COMMAND                  CREATED              STATUS              PORTS           NAMES
c39df819132f        yubuntu0109/yubuntu:0.1   "/bin/sh -c /bin/bash"   About a minute ago   Up About a minute   80/tcp          MyUbuntu18.04
ubuntu@VM-0-4-ubuntu:~$ sudo docker stop c39df819132f
c39df819132f
ubuntu@VM-0-4-ubuntu:~$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS       NAMES
ubuntu@VM-0-4-ubuntu:~$</code></pre></div>

<p>注意 : 若使用 <code>docker rmi &lt;Image-Id&gt;</code> 无法删除指定镜像，请参考如下解决方法 :</p>
<ol>
<li><p>使用 tag 删除( 若仍有镜像删除不掉，请使用法二 )</p>
<div class="hljs"><pre><code class="hljs shell">docker rmi -f [REPOSITORY]:[TAG] # -f : 强制删除</code></pre></div>
</li>
<li><p>彻底删除所有镜像 :</p>
<div class="hljs"><pre><code class="hljs shell">systemctl stop docker.service
rm -rf /var/lib/docker
systemctl start docker.service</code></pre></div>
</li>
</ol>

            </article>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/Docker/">Docker</a>
                    
                      <a class="hover-with-bg" href="/tags/Ubuntu/">Ubuntu</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" target="_blank" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！</p>
              
              
                <div class="post-prevnext row">
                  <div class="post-prev col-6">
                    
                    
                  </div>
                  <div class="post-next col-6">
                    
                    
                      <a href="/2020/06/26/Dockerfile%E4%BB%8B%E7%BB%8D%E5%8F%8A%E6%8C%87%E4%BB%A4%E8%AF%B4%E6%98%8E/">
                        <span class="hidden-mobile">Docker Dockerfile介绍及指令说明</span>
                        <span class="visible-mobile">Next</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </div>
                </div>
              
            </div>

            
          </div>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;TOC</p>
  <div id="tocbot"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    
  </main>

  
    <a id="scroll-top-button" href="#" role="button">
      <i class="iconfont icon-arrowup" aria-hidden="true"></i>
    </a>
  

  
    <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">Search</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">keyword</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
  

  

  

  <footer class="mt-5">
  <div class="text-center py-3">
    <div>
      <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a>
      <i class="iconfont icon-love"></i>
      <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener">
        <span>Fluid</span></a>
    </div>
    
  <div class="statistics">
    
    

    
      
        <!-- 不蒜子统计PV -->
        <span id="busuanzi_container_site_pv" style="display: none">
            总访问量 
            <span id="busuanzi_value_site_pv"></span>
             次
          </span>
      
      
        <!-- 不蒜子统计UV -->
        <span id="busuanzi_container_site_uv" style="display: none">
            总访客数 
            <span id="busuanzi_value_site_uv"></span>
             人
          </span>
      
    
  </div>


    

    
  </div>
</footer>

<!-- SCRIPTS -->
<script  src="https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js" ></script>
<script  src="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/js/bootstrap.min.js" ></script>
<script  src="/js/debouncer.js" ></script>
<script  src="/js/main.js" ></script>

<!-- Plugins -->


  
    <script  src="/js/lazyload.js" ></script>
  



  <script defer src="https://cdn.staticfile.org/clipboard.js/2.0.6/clipboard.min.js" ></script>
  <script  src="/js/clipboard-use.js" ></script>



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





  <script  src="https://cdn.staticfile.org/tocbot/4.11.1/tocbot.min.js" ></script>
  <script>
    $(document).ready(function () {
      var boardCtn = $('#board-ctn');
      var boardTop = boardCtn.offset().top;

      tocbot.init({
        tocSelector: '#tocbot',
        contentSelector: 'article.markdown-body',
        headingSelector: 'h1,h2,h3,h4,h5,h6',
        linkClass: 'tocbot-link',
        activeLinkClass: 'tocbot-active-link',
        listClass: 'tocbot-list',
        isCollapsedClass: 'tocbot-is-collapsed',
        collapsibleClass: 'tocbot-is-collapsible',
        collapseDepth: 0,
        scrollSmooth: true,
        headingsOffset: -boardTop
      });
      if ($('.toc-list-item').length > 0) {
        $('#toc').css('visibility', 'visible');
      }
    });
  </script>



  <script  src="https://cdn.staticfile.org/typed.js/2.0.11/typed.min.js" ></script>
  <script>
    var typed = new Typed('#subtitle', {
      strings: [
        '  ',
        "使用Dockerfile构建一个属于自己的Ubuntu&nbsp;",
      ],
      cursorChar: "_",
      typeSpeed: 70,
      loop: false,
    });
    typed.stop();
    $(document).ready(function () {
      $(".typed-cursor").addClass("h2");
      typed.start();
    });
  </script>



  <script  src="https://cdn.staticfile.org/anchor-js/4.2.2/anchor.min.js" ></script>
  <script>
    anchors.options = {
      placement: "right",
      visible: "hover",
      
    };
    var el = "h1,h2,h3,h4,h5,h6".split(",");
    var res = [];
    for (item of el) {
      res.push(".markdown-body > " + item)
    }
    anchors.add(res.join(", "))
  </script>



  <script  src="/js/local-search.js" ></script>
  <script>
    var path = "/local-search.xml";
    var inputArea = document.querySelector("#local-search-input");
    inputArea.onclick = function () {
      searchFunc(path, 'local-search-input', 'local-search-result');
      this.onclick = null
    }
  </script>



  <script  src="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.css" />

  <script>
    $('#post img:not(.no-zoom img, img[no-zoom]), img[zoom]').each(
      function () {
        var element = document.createElement('a');
        $(element).attr('data-fancybox', 'images');
        $(element).attr('href', $(this).attr('src'));
        $(this).wrap(element);
      }
    );
  </script>







  
  
    <script>
      !function (e, t, a) {
        function r() {
          for (var e = 0; e < s.length; e++) s[e].alpha <= 0 ? (t.body.removeChild(s[e].el), s.splice(e, 1)) : (s[e].y--, s[e].scale += .004, s[e].alpha -= .013, s[e].el.style.cssText = "left:" + s[e].x + "px;top:" + s[e].y + "px;opacity:" + s[e].alpha + ";transform:scale(" + s[e].scale + "," + s[e].scale + ") rotate(45deg);background:" + s[e].color + ";z-index:99999");
          requestAnimationFrame(r)
        }

        function n() {
          var t = "function" == typeof e.onclick && e.onclick;
          e.onclick = function (e) {
            t && t(), o(e)
          }
        }

        function o(e) {
          var a = t.createElement("div");
          a.className = "heart", s.push({
            el: a,
            x: e.clientX - 5,
            y: e.clientY - 5,
            scale: 1,
            alpha: 1,
            color: c()
          }), t.body.appendChild(a)
        }

        function i(e) {
          var a = t.createElement("style");
          a.type = "text/css";
          try {
            a.appendChild(t.createTextNode(e))
          } catch (t) {
            a.styleSheet.cssText = e
          }
          t.getElementsByTagName("head")[0].appendChild(a)
        }

        function c() {
          return "rgb(" + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + ")"
        }

        var s = [];
        e.requestAnimationFrame = e.requestAnimationFrame || e.webkitRequestAnimationFrame || e.mozRequestAnimationFrame || e.oRequestAnimationFrame || e.msRequestAnimationFrame || function (e) {
          setTimeout(e, 1e3 / 60)
        }, i(".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: fixed;}.heart:after{top: -5px;}.heart:before{left: -5px;}"), n(), r()
      }(window, document);
    </script>
  














</body>
</html>
